package com.winning.lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestLambdaDemo {

    List<Employee> emps = Arrays.asList(
            new Employee(101, "张三", 18, 9999.99),
            new Employee(102, "李四", 59, 6666.66),
            new Employee(103, "王五", 28, 3333.33),
            new Employee(104, "赵六", 8, 7777.77),
            new Employee(105, "田七", 38, 5555.55)
    );

    @Test
    public void test1(){
        Collections.sort(emps, (e1, e2) ->{
            if(e1.getAge() == e2.getAge()){
                return e1.getName().compareTo(e2.getName());
            }else{
                return -Integer.compare(e1.getAge(), e2.getAge());
            }
        });

        for (Employee emp: emps) {
            System.out.println(emp);
        }
    }

    @Test
    public void test2(){
        String result = strHandler("我打哈哈abcdEfg", x -> x.toUpperCase());
        System.out.println(result);

        result = strHandler("我打哈哈abcdEfg", x -> x.substring(2, 4));
        System.out.println(result);
    }


    public String strHandler(String str, MyFunction myFunction){
        return myFunction.getValue(str);
    }

    @Test
    public void test3(){
        System.out.println(calculation(100L,200L, (x, y) -> x + y));

        System.out.println(calculation(100L, 200L, (x, y) -> x * y));
    }

    public Long calculation(Long l1, Long l2, MyFunction2<Long, Long> mf){
        return mf.getValue(l1, l2);
    }

}
